export default class Scanner {
    constructor(templateStr) {
        this.templateStr = templateStr;
        //  指针
        this.pos = 0;
        //  尾巴(剩余字符)
        this.tail = templateStr;
    }

    //  收集解析符号
    scan(tag) {
        var match = this.tail.match(tag);

        if (!match || match.index !== 0)
            return '';

        var string = match[0];

        this.tail = this.tail.substring(string.length);
        this.pos += string.length;

        return string;
    }

    //  收集内容
    scanUntil(stopTag) {
        let index = this.tail.search(stopTag), match;

        switch (index) {
            case -1:
                match = this.tail;
                this.tail = '';
                break;
            // case 0:
            //     match = '';
            //     break;
            default:
                match = this.tail.substring(0, index);
                this.tail = this.tail.substring(index);
        }

        this.pos += match.length;

        return match;
    }
}

const str = 'testttt';
console.log(str.search('test'))